﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using XXN.Model.Model;
using XXN.Service;

namespace XXN.Admin.Controllers
{
    public class ProductController : Controller
    {
        ProductService pro = new ProductService();
        ProductTypeService protype = new ProductTypeService();
        // GET: Product
        public ActionResult Index()
        {
            return View();
        }
        public ActionResult GetProduct(int page, int limit, string productName, string productType)
        {
            var count = 0;
            var list = pro.GetProduct(page, limit, productName, productType, out count);
            return Json(new { code = 0, msg = "", count = count, data = list }, JsonRequestBehavior.AllowGet);
        }
        [HttpGet]
        public ActionResult AddInfo()
        {
            return View();
        }
        [HttpPost]
        public ActionResult AddInfo(Product pro1)
        {
            var n = pro.AddInfo(pro1);
            if (n > 0)
            {
                return Json(new { success = true, msg = "添加成功" });
            }
            return Json(new { success = false, msg = "添加失败" });
        }
        [HttpGet]
        public ActionResult EditInfo(int id)
        {
            var re = pro.EditInfo(id);
            var emptype = protype.GetProductTypeInfo();
            List<SelectListItem> type = new List<SelectListItem>();
            foreach (var item in emptype)
            {
                type.Add(new SelectListItem() { Value = item.Pro_cat_id.ToString(), Text = item.Pro_cat_name });
            }
            ViewData["type"] = type;
            return View(re);
        }
        [HttpPost]
        public ActionResult EditInfo(Product pro2)
        {
            var n = pro.EditInfo(pro2);
            if (n > 0)
            {
                return Json(new { success = true, msg = "编辑成功" });
            }
            return Json(new { success = false, msg = "编辑失败" });
        }
        public ActionResult DelInfo(int id)
        {
            int n = pro.DelInfo(id);
            return Content(n.ToString());
        }
        public ActionResult OnPorduct(int pro_id)
        {
            int re = pro.OnStatus(pro_id);
            return Content(re.ToString());
        }
        public ActionResult OffPorduct(int pro_id)
        {
            int re = pro.OffStatus(pro_id);
            return Content(re.ToString());
        }
        [HttpGet]
        public ActionResult RecommendInfo(int id)
        {
            var gdd = pro.EditInfo(id);
            var emptypes = pro.RecommendInfo();
            List<SelectListItem> recom = new List<SelectListItem>();
            foreach (var item in emptypes)
            {
                recom.Add(new SelectListItem() { Value = item.Rec_id.ToString(), Text = item.Positionname });
            }
            ViewData["recom"] = recom;
            return View(gdd);
        }
        [HttpPost]
        public ActionResult RecommendInfo(Product pro1)
        {
            var n = pro.RecommendPro(pro1);
            if (n > 0)
            {
                return Json(new { success = true, msg = "推荐成功" });
            }
            return Json(new { success = false, msg = "推荐失败" });
        }

    }
}